const ExtractTextPlugin   = require("extract-text-webpack-plugin");
const ReactHotLoader = require('react-hot-loader/patch');
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const sourcePath = path.join(__dirname, './../src');


//獲取html-webpack-plugin參數的方法
var getHtmlConfig = function(name , title){
    return{
        template : './src/views/' + name +'.html',
        filename :  name +'.html',
        favicon  : './favicon.ico',
        title    : title,
        inject 	 : true,
        hash	   : true,
        chunks	 : ['common',name]
    }
}

module.exports = {
    devtool: 'cheap-module-eval-source-map',

    entry: {
        // 'hot': 'webpack/hot/only-dev-server',
        // 'patch':'react-hot-loader/patch',
        // 'devServerClient': 'webpack-dev-server/client?http://localhost:9090',
        // 'index':'./src/pages/index.js',
        // // 'login':'./src/pages/login.js',

        index:[
            'webpack-dev-server/client?http://localhost:9090',
            'react-hot-loader/patch',
            'webpack/hot/only-dev-server',
            path.resolve(__dirname, '../src/pages/index.js'),
        ],
        login:[
            'webpack-dev-server/client?http://localhost:9090',
            'react-hot-loader/patch',
            'webpack/hot/only-dev-server',
            path.resolve(__dirname, '../src/pages/login.js'),
        ],
        register:[
            'webpack-dev-server/client?http://localhost:9090',
            'react-hot-loader/patch',
            'webpack/hot/only-dev-server',
            path.resolve(__dirname, '../src/pages/register.js'),
        ],
        findPassword:[
            'webpack-dev-server/client?http://localhost:9090',
            'react-hot-loader/patch',
            'webpack/hot/only-dev-server',
            path.resolve(__dirname, '../src/pages/findPassword.js'),
        ],
        findGoods:[
            'webpack-dev-server/client?http://localhost:9090',
            'react-hot-loader/patch',
            'webpack/hot/only-dev-server',
            path.resolve(__dirname, '../src/pages/findGoods.js'),
        ],
        saleGoods:[
            'webpack-dev-server/client?http://localhost:9090',
            'react-hot-loader/patch',
            'webpack/hot/only-dev-server',
            path.resolve(__dirname, '../src/pages/saleGoods.js'),
        ],
        wantGoods:[
            'webpack-dev-server/client?http://localhost:9090',
            'react-hot-loader/patch',
            'webpack/hot/only-dev-server',
            path.resolve(__dirname, '../src/pages/wantGoods.js'),
        ],

    }, //指定入口文件，程序从这里开始编译,__dirname当前所在目录, ../表示上一级目录, ./同级目录
    output: {
        // path: path.resolve(__dirname, '../dist'), // 输出的路径
        // filename: 'app/[name]_[hash:8].js'  // 打包后文件
        path: path.join(__dirname, '../dist'),
        filename:'app/[name].js',
        // publicPath属性
        // publicPath:'/dist/'
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                loader: 'babel-loader',//加载器
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: [{
                    loader: 'style-loader'
                }, {
                    loader: 'css-loader'
                }],
            },
            {
                test: /\.less$/,
                use: [{
                    loader: "style-loader" // creates style nodes from JS strings
                }, {
                    loader: "css-loader" // translates CSS into CommonJS
                }, {
                    loader: "less-loader" // compiles Less to CSS
                }]
            },
            {
                test: /\.(eot|woff|woff2|ttf)$/,
                exclude: /node_modules/,
                loader: "file-loader"
            },
            {
                test: /\.(svg|png|jpeg|jpg|ico)$/,
                loaders: ['url-loader?limit=10000']
            },
        ],
    },
    resolve: {
        extensions: ['.js', '.jsx'],
        modules: [
            sourcePath,
            'node_modules'
        ]
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        //把Css单独打包到文件里
        new ExtractTextPlugin("css/[name].css"),



        // new HtmlWebpackPlugin({
        //     template: path.resolve(__dirname, '../src/views/index.html'),
        //     inject: true,
        //     title    : "首页",
        //     favicon  : 'favicon.ico'
        // }),

        new HtmlWebpackPlugin(getHtmlConfig('index','首页')),
        new HtmlWebpackPlugin(getHtmlConfig('login','用户登录')),
        new HtmlWebpackPlugin(getHtmlConfig('register','用户注册')),
        new HtmlWebpackPlugin(getHtmlConfig('findPassword','找回密码')),
        new HtmlWebpackPlugin(getHtmlConfig('findGoods','求购专区')),
        new HtmlWebpackPlugin(getHtmlConfig('saleGoods','出售二货')),
        new HtmlWebpackPlugin(getHtmlConfig('wantGoods','求售二货')),
    ]
}